package com.xxcxcxcx.jobsystem.common.Job.Herdsman;

import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.Job.JobBase;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapability;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.util.LazyOptional;

public class Herdsman extends JobBase {
    public static String Job= JobsClass.Herdsman;
    public static final DamageSource ANIMAL = new DamageSource("Animal");
    public static void TouchEntity(Player player, Entity entity){
        if(entity instanceof Animal){
            LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
            if(!capability.isPresent()) {
                return;
            }
            PlayerSaveData playerSaveData = capability.orElse(null);
            int level = playerSaveData.getLevel(Job),a=0;
            if(level<4){
                if(player.getRandom().nextDouble()<(1-level*0.2)){
                    player.hurt(DamageSource.mobAttack((LivingEntity) entity), a=player.getRandom().nextInt(1,5-level));
                }
                UpExperience(player,a*0.7f,Job);
            }
        }
    }
    public static int GetDrop(Player player,Entity entity){
        int num=1;
        if(InAnimal(entity)){
            LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
            if(!capability.isPresent()) {
                return 1;
            }
            PlayerSaveData playerSaveData = capability.orElse(null);
            int level =playerSaveData.getLevel(Job);
            if(level<4){
                num -= (player.getRandom().nextDouble()<(1 - 0.25 * level)?1:0);
            }
            if(level>=4){
                double z = (0.2 * (level-3));
                double r=player.getRandom().nextDouble();
                while (z>r){
                    num++;
                    z-=r;
                    r=player.getRandom().nextDouble();
                }
            }
            UpExperience(player,Math.max(num,1)*7,Job);
        }
        if(InMonster(entity)){
            LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
            if(!capability.isPresent()) {
                return 1;
            }
            PlayerSaveData playerSaveData = capability.orElse(null);
            int level =playerSaveData.getLevel(Job);
            if(level<4){
                num -= (player.getRandom().nextDouble()<(1 - 0.125 * level)?1:0);
            }
            if(level>=4){
                double z = (0.1+0.05 * (level-3));
                double r=player.getRandom().nextDouble();
                while (z>r){
                    num++;
                    z-=r;
                    r=player.getRandom().nextDouble();
                }
            }
            if(level<4)UpExperience(player,Math.max(num,1)*2,Job);
        }
        return num;
    }

    public static boolean RightMob(Entity entity){
        return InMonster(entity)||InAnimal(entity);
    }
    public static boolean InMonster(Entity entity){
        return entity instanceof Monster;
    }
    public static boolean InAnimal(Entity entity){
        return entity instanceof Animal;
    }

    public static int MoreBaby(Player player) {
        LazyOptional<JobCapability> capability = JobCapabilityProvider.getPlayerData(player);
        if(!capability.isPresent()) {
            return 0;
        }
        PlayerSaveData playerSaveData = capability.orElse(null);
        int level =playerSaveData.getLevel(Job);
        int num=0;
        if(level>=4){
            num += (player.getRandom().nextDouble()<(0.1 + 0.03  * level)?1:0);
        }
        if(level>=7){
            num += (player.getRandom().nextDouble()<(0.2 + 0.05  * (level-6))?1:0);
        }
        if(num>0)UpExperience(player,num*20,Job);
        else UpExperience(player,4,Job);
        return num;
    }
}
